package cn.edu.hqu.javae.web;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.edu.hqu.javae.domain.Course;
import cn.edu.hqu.javae.domain.Course.Type;
import cn.edu.hqu.javae.domain.Xk;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/choose")
public class XkController {
	@ModelAttribute//��ע�����εķ��������п���������ִ��ǰ����ִ��
	public void addIngredientsToModel(Model model) {
		List<Course> courses = Arrays.asList(
				  new Course("软件工程", "软件工程", Type.SPECIALIZED),
				  new Course("数据库", "数据库", Type.SPECIALIZED),
				  new Course("数据结构", "数据结构", Type.OPTIONAL),
				  new Course("高等代数", "高等代数", Type.OPTIONAL)
				  
			
				);
		Type[] types = Course.Type.values();
		for (Type type : types) {
		  model.addAttribute(type.toString().toLowerCase(),
		      filterByType(courses, type));//��KEY-VALUE��ʽ��������ӵ�model
		}
	}
	@GetMapping
	public String showDesignForm(Model model) {
		model.addAttribute("xk",new Xk());
		return "xk";

	}

	@PostMapping//����POST����,@ModelAttributeע���Զ���model�е�����ע�뵽���εĲ���
	public String processDesing(@Valid @ModelAttribute("xk") Xk xk,Errors errors,Model model) {
		if (errors.hasErrors()) {
			return "xk";
		}
		log.info("Processing design:"+xk);
		return "xkzs";
		
		
	}
	 private List<Course> filterByType(
		      List<Course> courses, Type type) {
		    return courses
		              .stream()
		              .filter(x -> x.getType().equals(type))
		              .collect(Collectors.toList());
		  }


}
